package com.my.admin.handler;

import com.my.admin.enmu.ErrorTypeEnum;
import com.my.admin.util.Rest;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.beans.ConversionNotSupportedException;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;

/**
 * @author my
 * 全局异常处理
 */

@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {

    @ExceptionHandler(RuntimeException.class)
    public Rest runtimeException(RuntimeException ex) {
        ex.printStackTrace();
        return Rest.err(ErrorTypeEnum.ERROR_500, ex.getMessage());
    }

    @ExceptionHandler(NullPointerException.class)
    public Rest nullPointerException(NullPointerException ex) {
        ex.printStackTrace();
        return Rest.err(ErrorTypeEnum.ERROR_500, ex.getMessage());
    }

    @ExceptionHandler(ClassCastException.class)
    public Rest classCastException(ClassCastException ex) {
        ex.printStackTrace();
        return Rest.err(ErrorTypeEnum.ERROR_500, ex.getMessage());
    }

    @ExceptionHandler(IOException.class)
    public Rest ioException(IOException ex) {
        ex.printStackTrace();
        return Rest.err(ErrorTypeEnum.ERROR_500, ex.getMessage());
    }

    @ExceptionHandler(NoSuchMethodException.class)
    public Rest noSuchMethodException(NoSuchMethodException ex) {
        ex.printStackTrace();
        return Rest.err(ErrorTypeEnum.ERROR_500, ex.getMessage());
    }

    @ExceptionHandler(IndexOutOfBoundsException.class)
    public Rest indexOutOfBoundsException(IndexOutOfBoundsException ex) {
        ex.printStackTrace();
        return Rest.err(ErrorTypeEnum.ERROR_500, ex.getMessage());
    }

    @ExceptionHandler(HttpMessageNotReadableException.class)
    public Rest httpMessageNotReadableException(HttpMessageNotReadableException ex) {
        ex.printStackTrace();
        return Rest.err(ErrorTypeEnum.ERROR_400, ex.getMessage());
    }

    @ExceptionHandler(TypeMismatchException.class)
    public Rest typeMismatchException(TypeMismatchException ex) {
        ex.printStackTrace();
        return Rest.err(ErrorTypeEnum.ERROR_400, ex.getMessage());
    }

    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public Rest httpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException ex) {
        ex.printStackTrace();
        return Rest.err(ErrorTypeEnum.ERROR_500, ex.getMessage());
    }

    @ExceptionHandler(ConversionNotSupportedException.class)
    public Rest conversionNotSupportedException(ConversionNotSupportedException ex) {
        ex.printStackTrace();
        return Rest.err(ErrorTypeEnum.ERROR_500, ex.getMessage());
    }

    @ExceptionHandler(StackOverflowError.class)
    public Rest stackOverflowError(StackOverflowError ex) {
        ex.printStackTrace();
        return Rest.err(ErrorTypeEnum.ERROR_500, ex.getMessage());
    }

    @ExceptionHandler(ArithmeticException.class)
    public Rest arithmeticException(ArithmeticException ex) {
        ex.printStackTrace();
        return Rest.err(ErrorTypeEnum.ERROR_500, ex.getMessage());
    }

    @ExceptionHandler(UnknownAccountException.class)
    public Rest unknownAccountException(UnknownAccountException ex) {
        return Rest.err("该用户不存在");
    }

    @ExceptionHandler(DisabledAccountException.class)
    public Rest disabledAccountException(DisabledAccountException ex) {
        return Rest.err("该用户已被禁用");
    }

    @ExceptionHandler(CredentialsException.class)
    public Rest credentialsException(CredentialsException ex) {
        return Rest.err("用户名或密码不正确");
    }

    @ExceptionHandler(AccountException.class)
    public Rest accountException(AccountException ex) {
        return Rest.err("用户名或密码不正确");
    }

    @ExceptionHandler(IncorrectCredentialsException.class)
    public Rest accountException(IncorrectCredentialsException ex) {
        return Rest.err(ErrorTypeEnum.ERROR_401, ex.getMessage());
    }

    @ExceptionHandler(AuthorizationException.class)
    public Rest accountException(AuthorizationException ex) {
        String message = ex.getMessage();
        String msg=message.substring(message.indexOf("[")+1,message.indexOf("]"));
        if(message.contains("role")) {
            return Rest.err(ErrorTypeEnum.ERROR_402, "对不起，您没有" + msg + "角色！");
        }else if(message.contains("permission")) {
            return Rest.err(ErrorTypeEnum.ERROR_402, "对不起，您没有" + msg + "权限！");
        }else {
            return Rest.err(ErrorTypeEnum.ERROR_402, "对不起，您的权限有误！");
        }
    }

    @ExceptionHandler(Exception.class)
    public Rest exception(Exception ex) {
        ex.printStackTrace();
        return Rest.err(ErrorTypeEnum.ERROR_500, ex.getMessage());
    }

}
